package com.yimi.yinhepda.views.message;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.MyMessageDao;
import com.yimi.yinhepda.db.dao.UserReadMessageDao;
import com.yimi.yinhepda.entity.message.MyMessageBean;
import com.yimi.yinhepda.entity.message.UserReadMessageBean;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.DateHelper;

import java.sql.SQLException;
import java.util.Date;

/**
 * 消息详细界面
 *
 * @author 000432
 * @date 2018/1/13 0013
 */
public class MessageDetailActivity extends BaseActivity {

    public static final String MESSAGE_NOTIFICATION_ID = "message_notification_id";
    private MyMessageDao myMessageDao;
    private UserReadMessageDao readMessageDao;
    /**
     * 主标题
     */
    private TextView mTvMasterTitle;
    /**
     * 标题
     */
    private TextView mTvTitle;
    /**
     * 推送时间
     */
    private TextView mTvPushTime;
    /**
     * 文本内容
     */
    private TextView mTvContent;
    /**
     * 返回按钮
     */
    private Button mBtnBack;

    @Override
    public String getPageName() {
        return "消息详细界面";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_detail);

        mTvMasterTitle = (TextView) findViewById(R.id.msg_detail_master_title);
        mTvTitle = (TextView) findViewById(R.id.msg_detail_title);
        mTvPushTime = (TextView) findViewById(R.id.msg_detail_push_time);
        mTvContent = (TextView) findViewById(R.id.msg_detail_content);
        mBtnBack = (Button) findViewById(R.id.msg_detail_back);

        mBtnBack.setOnClickListener(this);

        myMessageDao = new MyMessageDao();
        readMessageDao = new UserReadMessageDao();

        Bundle bundle = getIntent().getExtras();
        String notfyId = bundle.getString(MESSAGE_NOTIFICATION_ID, "");
        MyMessageBean bean = myMessageDao.getDataByNotFyId(notfyId);
        if (bean == null) {
            return;
        }
        String titleStr = bean.getTitle();
        if (titleStr == null) {
            return;
        }

        if (titleStr.length() > 10) {
            titleStr = titleStr.substring(0, 10) + "...";
        }
        mTvMasterTitle.setText(titleStr);
        mTvTitle.setText(bean.getTitle());
        mTvPushTime.setText(DateHelper.DateToStr(DateHelper.StrToDate(bean.getPushTime(), DateHelper.timeFormat), DateHelper.timeFormat2));
        mTvContent.setText(bean.getContent());

        //设置该消息为已读
        String compCode = sharedPreferUtil.getValue(Common.COMP_CODE);
        String account = sharedPreferUtil.getWorkNum();
        String operator = sharedPreferUtil.getValue(Common.USER_CODE);
        if (!readMessageDao.exitReadMessage(operator, notfyId)) {
            UserReadMessageBean readMessageBean = new UserReadMessageBean();
            readMessageBean.setWorkNum(account);
            readMessageBean.setCompCode(compCode);
            readMessageBean.setOperator(operator);
            readMessageBean.setNotificationId(notfyId);
            readMessageBean.setCreateTime(DateHelper.getDateTimeFormate(new Date()));
            try {
                readMessageDao.insert(readMessageBean);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JPushInterface.clearNotificationById(this, Integer.parseInt(notfyId));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.msg_detail_back:
                finish();
                break;
            default:
                break;
        }
    }
}
